//
//  NiuModel.swift
//  UUCartoon
//
//  Created by Galaxy on 2024/2/2.
//  Copyright © 2024 qykj. All rights reserved.
//

import UIKit
import Ji
class NiuModel: WebsiteBaseModel,WebsiteProtocol {
    override init() {
        super.init()
        websiteName = "牛漫画"
        webUrlStr = "https://niumy.cc/"
        websiteTitleArr = []
        websiteIdArr = []
        type = .niu
    }
    //MARK: 排行列表
    func getCategorySiftList(condition: (category:String,reader:String,progress:String,area:String), pageNum: Int) -> [CartoonModel] {
        []
    }
    //MARK: 获取图片列表
    func getImageList(detailUrl: String) -> [CartoonImgModel] {
        let jiDoc = Ji.init(htmlURL: URL.init(string: detailUrl)!)
        if jiDoc == nil {
            return []
        }else{
            var array:[CartoonImgModel] = []
            // 获取页码
            let pageXpath = "//*[@id='content']/div[1]/div[3]/div[2]/a"
            let pageNodeArr = jiDoc?.xPath(pageXpath)
            let pageNum = pageNodeArr!.count + 1
            for item in 1...pageNum {
                let pageUrl = detailUrl+"/\(item)"
                let pageJiDoc = Ji.init(htmlURL: URL.init(string: pageUrl)!)
                let imgXpath = "//*[@id='post_content']/p/img/@src"
                let imgNodeArr = pageJiDoc?.xPath(imgXpath)
                for img in imgNodeArr! {
                    var imgModel = CartoonImgModel.init()
                    var imgUrl = img.content!
                    imgUrl = imgUrl.replacingOccurrences(of: "\\", with: "")
                    imgModel.imgUrl = imgUrl
                    imgModel.type = .niu
                    array.append(imgModel)
                }
            }
            return array
        }
    }
    //MARK: 搜索结果
    func getSearchList(keyword: String, pageNum: Int) -> [CartoonModel] {
        []
    }
    //MARK: 搜索推荐
    func getSearchRecommendList() -> [CartoonModel] {
        []
    }
    //MARK: 获取漫画详情
    func getDetailData(urlStr: String) -> CartoonModel {
        CartoonModel.init()
    }
    //MARK: 已完成
    func getDoneList(pageNum: Int) -> [CartoonModel] {
        []
    }
    //MARK: 漫画类型
    func getCategoryList() -> [[CategoryModel]] {
        []
    }
    //MARK: 漫画排行
    func getRankList(pageNum: Int) -> [CartoonModel] {
        []
    }
    //MARK: 最新更新
    func getLatestList(pageNum:Int) -> [CartoonModel] {
        let urlStr = webUrlStr + "index.php/page/\(pageNum)"
        let  jiDoc = Ji.init(htmlURL: URL.init(string: urlStr)!)
        if jiDoc == nil {
            return []
        } else {
            var resultArr: [CartoonModel] = []
            //            标题
            let titleXPath = "//*[@id='post_container']/li/div[1]/a/@title"
            //            详情
            let urlXPath = "//*[@id='post_container']/li/div[1]/a/@href"
            // 作者
            let authorXPath = "//*[@id='post_container']/li/div[3]/span[4]/a"
            // 时间
            let timeXPath = "//*[@id='post_container']/li/div[3]/span[2]"
            // 图片
            let imgXPath = "//*[@id='post_container']/li/div[1]/a/img/@src"
            // 最新
            let numXPath = "//*[@id='post_container']/li/div[3]/span[1]"
            let titleNodeArr = jiDoc?.xPath(titleXPath)
            let urlNodeArr = jiDoc?.xPath(urlXPath)
            let authorNodeArr = jiDoc?.xPath(authorXPath)
            let timeNodeArr = jiDoc?.xPath(timeXPath)
            let numNodeArr = jiDoc?.xPath(numXPath)
            let imgNodeArr = jiDoc?.xPath(imgXPath)
            // 数据不为空
            if !(urlNodeArr?.isEmpty)! {
                for (index, urlNode) in urlNodeArr!.enumerated() {
                    let cartoonModel = CartoonModel.init()
                    cartoonModel.name = titleNodeArr![index].content!
                    cartoonModel.detailUrl = urlNode.content!
                    cartoonModel.author = authorNodeArr![index].content!
                    cartoonModel.category = ""
                    cartoonModel.time = timeNodeArr![index].content!
                    let num = numNodeArr![index].content!
                    cartoonModel.type = .niu
                    cartoonModel.num = Tool.cleanChater(string: num)
                    cartoonModel.imgUrl = imgNodeArr![index].content!
                    let chapterModel = ChapterModel()
                    chapterModel.name = "默认"
                    chapterModel.detailUrl = urlNode.content!
                    cartoonModel.chapterArr = [
                        (name:"默认",data:[chapterModel])
                    ]
                    resultArr.append(cartoonModel)
                }
            }
            return resultArr
        }
    }
}
